package com.le.sarrs.web.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import reactor.function.Consumer;
import reactor.rx.Promise;

@SuppressWarnings("hiding")
public class HandleErrorConsumer<Exception> implements Consumer<Throwable> {
    private static final Logger logger = LoggerFactory.getLogger(HandleErrorConsumer.class);

    private Promise<ResponseEntity<String>> promise = null;
    private String handlerName = "";

    //
    public HandleErrorConsumer(Promise<ResponseEntity<String>> promise) {
        super();
        this.promise = promise;
    }

    @Override
    public void accept(Throwable t) {
        MultiValueMap<String, String> headers = new HttpHeaders();
        headers.add("Content-Type", "application/json; charset=UTF-8");
        logger.error(handlerName + ":" + String.valueOf(t));
        promise.accept(new ResponseEntity<>("{\"code\":" + 500 + "}", headers, HttpStatus.OK));
        return;
    }

}
